/**
 * 
 */
package com.ideazd.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ideazd.api.model.InterfaceLayout;
import com.ideazd.api.model.NewDetails;
import com.ideazd.api.model.Propaganda;
import com.ideazd.api.model.Topic;
import com.ideazd.api.model.User;
import com.ideazd.api.model.dto.RetDTO;
import com.ideazd.api.service.NewsDetailsService;
import com.ideazd.api.service.PropagandaService;
import com.ideazd.api.service.TopicService;
import com.ideazd.api.service.UserService;
import com.ideazd.api.tools.SysUtil;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author yzz
 * @create 2016年3月25日 下午5:16:10
 */
@ControllerBind(controllerKey="home")
public class HomeController extends Controller {
	private static final Logger log = Logger.getLogger(PropagandaController.class);
	private UserService userService = new UserService();
	private TopicService topicService = new TopicService();
	private PropagandaService propagandaService = new PropagandaService();
	private NewsDetailsService newsDetailsService = new NewsDetailsService();
	
	/**
	 * 首页接口
	 */
	public void getHome(){
		String useId = getPara("useId");
		Map<Object, Object> ret = new HashMap<Object,Object>();
		Map<Object, Object> data = new HashMap<Object,Object>();
		
		try {
			User user = new User();
			boolean unread = false;
			
			if(!StrKit.isBlank(useId)){
				user = userService.findUserById(useId);
				Topic post = Topic.dao.findFirst("select sum(unread) as count from sta_topic where createid = '"+useId+"'");
				
				if(post.get("count") != null && Double.parseDouble(post.get("count").toString()) > 0){
					unread = true;
				}
			}
			
			Map<Object, Object> userData = new HashMap<Object,Object>();
			
			userData.put("useId", SysUtil.validateStr(user.get("useid")));
			userData.put("account", SysUtil.validateStr(user.get("account")));
			userData.put("nickName", SysUtil.validateStr(user.get("nickname")));
			userData.put("gender", SysUtil.validateStr(user.get("gender")));
			userData.put("headPortrait", SysUtil.validateStr(user.get("headportrait")));
			userData.put("userStatus", SysUtil.validateStr(user.get("userstatus")));
			userData.put("useType", SysUtil.validateStr(user.get("usetype")));
			userData.put("unread", unread);
			data.put("user", userData);
			
			
			List <Propaganda> propagandaList = propagandaService.getPropagandaList("2");
		    List<Map<Object, Object>> propagandaDataList = new ArrayList<Map<Object, Object>>();
			
			for(Propaganda propaganda : propagandaList){
				Map<Object, Object> dataMap = new HashMap<Object,Object>();
				dataMap.put("picAddress", PropKit.get("server_uri") + propaganda.get("picaddress"));
				dataMap.put("operationEvent", propaganda.get("operationevent"));
				dataMap.put("eventInfo", propaganda.get("eventinfo"));
				propagandaDataList.add(dataMap);
			}
			
			data.put("bannerPics", propagandaDataList);
			
			List <InterfaceLayout> layoutList = InterfaceLayout.dao.find("select * from sta_interface_layout where interfacename = '1'");
			List<Map<Object, Object>> layoutDataList = new ArrayList<Map<Object, Object>>();
			
			for(InterfaceLayout layout : layoutList){
				Map<Object, Object> dataMap = new HashMap<Object,Object>();
				dataMap.put("interfaceUrl", layout.get("interfaceurl"));
				layoutDataList.add(dataMap);
			}
			
			data.put("viewPage", layoutDataList);
			
			Page<Topic> topicPage = topicService.getTopicPage("1", "","commentamount");
			
			List<Map<Object, Object>> topicList = new ArrayList<Map<Object, Object>>();
			
			for(Topic top : topicPage.getList()){
				Map<Object, Object> topic = new HashMap<Object,Object>();
				topic.put("topId", top.get("topid"));
				topic.put("topTitle", top.get("toptitle"));
				topic.put("listPicture", PropKit.get("server_uri") + top.get("listpicture"));
				topic.put("topPicture", PropKit.get("server_uri") +  SysUtil.validateStr(top.get("toppicture")));
				topic.put("readAmount", top.get("readamount"));
				topic.put("clickAmount", top.get("clickamount"));
				topicList.add(topic);
			}
			
			data.put("topicList", topicList);
			
			Page<NewDetails> newDetails = newsDetailsService.newDetailsList("1","createtime",null);
			
			List<Map<Object, Object>> newList = new ArrayList<Map<Object, Object>>();
			
			for(NewDetails newDetail : newDetails.getList()){
				Map<Object, Object> news = new HashMap<Object,Object>();
				news.put("detId", newDetail.get("detid"));
				news.put("detType", newDetail.get("dettype"));
				news.put("createTime", newDetail.get("createtime").toString().substring(0, 10));
				news.put("detTitle", newDetail.get("dettitle"));
				news.put("detSimpleTitle", newDetail.get("dettitle"));
				news.put("detPicture", PropKit.get("server_uri") +  newDetail.get("detpicture"));
				newList.add(news);
			}
			
			data.put("newsList", newList);
			
			ret.put("data", data);
			ret.put("code","0");
			ret.put("msg","成功");
			
			
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
	}
	
	public void switchForIOS(){
		RetDTO ret = new RetDTO();
		ret.setCode("0");
		ret.put("status", "0");
		ret.setMsg("成功");
		renderJson(ret);
		return;
		
	}

}
